package org.jeecg.modules.gwcjava.jiekou.controller;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.oss.OSSClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CacheConstant;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.common.util.MD5Util;
import org.jeecg.common.util.PasswordUtil;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.gwcjava.jiekou.entity.*;
import org.jeecg.modules.gwcjava.jiekou.service.MlccUserInfoService;
import org.jeecg.modules.gwcjava.util.FileNameUtils;
import org.jeecg.modules.gwcjava.util.SmsUtils;
import org.jeecg.modules.system.model.SysLoginModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.net.UnknownHostException;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.*;

/**
 * @Description: 用户端接口
 * @Author: jeecg-boot
 * @Date:   2020-05-16
 * @Version: V1.0
 */
@Api(tags="用户端接口")
@RestController
@RequestMapping("/user/interface")
@Slf4j
public class MlccUserInfoController {
    private final static String ENDPOINT = "http://oss-cn-shanghai.aliyuncs.com";
    private final static String ACCESSKEYID = "LTAI4GEZ1VRo4geGJMJETXfM";
    private final static String ACCESSKEYSECRET = "aazkmD8DoXmV9qgT7o6BL8ZaMMj3W1";
    private final static String BUCKETNAME = "mlcc-public-001";
    private final static String KEY = "mlcc/";

    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private ISysBaseAPI sysBaseAPI;
    @Autowired
    private MlccUserInfoService userservice;

    /**
     * 登录接口
     * @param
     * @return boolean
     */
    @PostMapping(value = "/login")
    @ResponseBody
    public JSONObject post_login(
            @RequestParam(name="username") String username,
            @RequestParam(name="password") String password) {
        System.out.println(username + "=zhoud走的密码登录==" + password);
        //根据前端传入的值返回json数据
        JSONObject obj = new JSONObject();//返回对象
        try {
            if(username==null || "".equals(username)){
                obj.put("code", 1);
                obj.put("msg", "用户名不能为空");
                return obj;
            }
            if(password==null || "".equals(password)){
                obj.put("code", 2);
                obj.put("msg", "账户密码不能为空");
                return obj;
            }
            //根据用户名去查询用户是否存在
            MlccUserInfo userInfo = userservice.queryUserinfoByusername(username,1);
            if(userInfo==null){
                obj.put("code", 3);
                obj.put("msg", "该用户不存在");
                return obj;
            }else {
                //2. 校验用户名或密码是否正确
                String userpassword = PasswordUtil.encrypt(userInfo.getWorkNumber(), password, userInfo.getSalt());
                String syspassword = userInfo.getPassword();
                if (!userpassword.equals(syspassword)) {
                    obj.put("code", 4);
                    obj.put("msg", "账户密码错误");
                    return obj;
                }
                if (userInfo.getStatus()==1) {
                    obj.put("code", 4);
                    obj.put("msg", "您的账户已被冻结或禁用，请联系管理员");
                    return obj;
                }
                if (userInfo.getStatus()==2) {
                    obj.put("code", 4);
                    obj.put("msg", "您的账户已被删除，请联系管理员");
                    return obj;
                }

                // 添加日志
                sysBaseAPI.addLog("账户登录用户名: " + userInfo.getUsername() + "登录成功！", CommonConstant.LOG_TYPE_1,
                        null);
                // 生成token
                String token = JwtUtil.sign(userInfo.getUsername(), userInfo.getPassword());
                //更新用户的token
                userservice.update_token(token,username,1);
                // 设置token缓存有效时间
                redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + token, token);
                redisUtil.expire(CommonConstant.PREFIX_USER_TOKEN + token, JwtUtil.EXPIRE_TIME*2 / 1000);
                userInfo.setToken(token);
                System.out.println("token是2222======" + token);
                userInfo.setPayPassword("");
                obj.put("code", 0);
                obj.put("msg", "登录成功，欢迎使用！");
                obj.put("data", userInfo);
                return obj;
            }
        }catch (Exception e){
            obj.put("code", 4);
            obj.put("msg", "您的账户暂未设置密码，请用短信登录");
            return obj;
        }
    }



    /**
     * 手机密码登录接口
     * @param
     * @return boolean
     */
    @PostMapping(value = "/phone_password_login")
    @ResponseBody
    public JSONObject post_phone_password_login(
            @RequestParam(name="username") String username,
            @RequestParam(name="password") String password) {
        System.out.println(username + "===" + password);
        //根据前端传入的值返回json数据
        JSONObject obj = new JSONObject();//返回对象
        try {
            if(username==null || "".equals(username)){
                obj.put("code", 1);
                obj.put("msg", "用户名不能为空");
                return obj;
            }
            if(password==null || "".equals(password)){
                obj.put("code", 2);
                obj.put("msg", "账户密码不能为空");
                return obj;
            }
            MlccUserInfo userInfo_phone = userservice.queryUserinfoByusername(username,2);
            if(userInfo_phone==null){
                obj.put("code", 3);
                obj.put("msg", "该手机号暂未注册");
                return obj;
            }else {
                //2. 校验用户名或密码是否正确
                String userpassword = PasswordUtil.encrypt(userInfo_phone.getWorkNumber(), password, userInfo_phone.getSalt());
                String syspassword = userInfo_phone.getPassword();
                if (!userpassword.equals(syspassword)) {
                    obj.put("code", 4);
                    obj.put("msg", "账户密码错误");
                    return obj;
                }
                if (userInfo_phone.getStatus()==1) {
                    obj.put("code", 4);
                    obj.put("msg", "您的账户已被冻结或禁用，请联系管理员");
                    return obj;
                }
                if (userInfo_phone.getStatus()==2) {
                    obj.put("code", 4);
                    obj.put("msg", "您的账户已被删除，请联系管理员");
                    return obj;
                }

                // 添加日志
                sysBaseAPI.addLog("账户登录用户名: " + userInfo_phone.getUsername() + "登录成功！", CommonConstant.LOG_TYPE_1,
                        null);
                // 生成token
                Long startTs = System.currentTimeMillis(); // 当前时间戳
                String token = JwtUtil.sign(userInfo_phone.getUsername()+startTs, userInfo_phone.getPassword());
                //更新用户的token
                userservice.update_token(token,username,2);
                // 设置token缓存有效时间
                redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + token, token);
                redisUtil.expire(CommonConstant.PREFIX_USER_TOKEN + token, JwtUtil.EXPIRE_TIME*2 / 1000);
                userInfo_phone.setToken(token);
                System.out.println("手机密码登录的token======" + token);
                userInfo_phone.setPayPassword("");
                obj.put("code", 0);
                obj.put("msg", "登录成功，欢迎使用！");
                obj.put("data", userInfo_phone);
                return obj;
            }
        }catch (Exception e){
            obj.put("code", 4);
            obj.put("msg", "您的账户暂未设置密码，请用短信登录");
            return obj;
        }
    }


    /**
     * 手机号登录
     * @param
     * @return boolean
     */
    @PostMapping(value = "/phone_login")
    @ResponseBody
    public JSONObject post_phone_login(String phone,String code) {
        System.out.println(phone + "===" + code);
        //根据前端传入的值返回json数据
        JSONObject obj = new JSONObject();//返回对象
        if(phone==null || "".equals(phone)){
            obj.put("code", 1);
            obj.put("msg", "手机号不能为空");
            return obj;
        }
        if(code==null || "".equals(code)){
            obj.put("code", 2);
            obj.put("msg", "验证码不能为空");
            return obj;
        }
        System.out.println("手机号_缓存=" + redisUtil.get(phone));
        if (!code.equals(redisUtil.get(phone))) {
            obj.put("msg", "手机验证码错误");
            obj.put("code", 3);
            return obj;
        }

        //根据手机号去查询用户是否存在
        MlccUserInfo userInfo = userservice.queryUserinfoByusername(phone,2);
        if(userInfo==null){
            obj.put("code", 3);
            obj.put("msg", "该用户不存在");
            return obj;
        }else {
            if (userInfo.getStatus()==1) {
                obj.put("code", 4);
                obj.put("msg", "您的账户已被冻结或禁用，请联系管理员");
                return obj;
            }
            if (userInfo.getStatus()==2) {
                obj.put("code", 4);
                obj.put("msg", "您的账户已被删除，请联系管理员");
                return obj;
            }

            // 添加日志
            sysBaseAPI.addLog("手机号登录用户: " + userInfo.getUsername() + "登录成功！", CommonConstant.LOG_TYPE_1,
                    null);
            // 生成token
            Long startTs = System.currentTimeMillis(); // 当前时间戳
            String token = JwtUtil.sign(userInfo.getUsername()+startTs, userInfo.getPassword());
            //更新用户的token
            userservice.update_token(token,phone,2);
            // 设置token缓存有效时间
            redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + token, token);
            redisUtil.expire(CommonConstant.PREFIX_USER_TOKEN + token, JwtUtil.EXPIRE_TIME*2 / 1000);
            userInfo.setToken(token);
            obj.put("code", 0);
            obj.put("msg", "登录成功，欢迎使用！");
            obj.put("data", userInfo);
            return obj;
        }
    }

    /**
     * 发送验证码短信
     *
     * @return
     * @throws
     */
    @RequestMapping(value = "/getCaptcha")
    public JSONObject getCaptcha(String phone) {
        JSONObject obj = new JSONObject();
        if(phone==null || "".equals(phone)){
            obj.put("data", "");
            obj.put("code", 2);
            obj.put("msg", "手机号不能为空");
            return obj;
        }
        //根据手机号去查询用户是否存在
        MlccUserInfo userInfo = userservice.queryUserinfoByusername(phone,2);
        if(userInfo==null){
            obj.put("data", "");
            obj.put("code", 3);
            obj.put("msg", "该用户不存在，请先注册账号");
            return obj;
        }
        String captcha = SmsUtils.getPhonemsg(phone);
        // 将验证码存到Redis中
        redisUtil.set(phone, captcha);
        // redisUtil.set(phone, phone);
        redisUtil.expire(captcha, 300);// 设置清空时间为5分钟
        System.out.println("验证码===" + captcha);
        if (captcha != null) {
            /* obj.put("data", captcha); */
            obj.put("data", captcha);
            obj.put("code", 0);
            obj.put("msg", "短信发送成功");
            return obj;
        } else {
            obj.put("data", captcha);
            obj.put("code", 1);
            obj.put("msg", "短信发送失败");
            return obj;
        }
    }

    /**
     * 根据token查询用户详细信息
     * @return
     */
    @PostMapping(value = "/query_userInfoBytoken")
    public JSONObject query_query_userInfoBytoken(String token){
        JSONObject obj = new JSONObject();//返回对象
        MlccUserInfo userInfo = userservice.queryUserinfoByusername(token,3);
        if(userInfo==null){
            obj.put("code", 4);
            obj.put("msg", "token已失效，请重新登录");
            return obj;
        }else {
            //解密
//            System.out.print("=="+userInfo.getWorkNumber()+"==="+userInfo.getPassword()+"=="+userInfo.getSalt());
//            String userpassword = PasswordUtil.decrypt(userInfo.getUsername(), userInfo.getPassword(), userInfo.getSalt());
//            userInfo.setShowpwd(userpassword);

//            double debtLimit = userInfo.getDebtLimit().setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
//            double remainingLimit = userInfo.getRemainingLimit().setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
//            double deductionLimit = userInfo.getDeductionLimit().setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
//            DecimalFormat df = new DecimalFormat("0.00");
//            userInfo.setAccountBalance(df.format(Double.parseDouble(userInfo.getAccountBalance())));
//            userInfo.setMonthLines(df.format(Double.parseDouble(userInfo.getMonthLines())));
//            userInfo.setShengyuMoeny(df.format(userInfo.getRemainingLimit()));
//            userInfo.setDikouMoeny(df.format(userInfo.getDeductionLimit()));
//            userInfo.setFuzaiMoeny(df.format(userInfo.getDebtLimit()));
            obj.put("code", 0);
            obj.put("msg", "查询成功");
            obj.put("data", userInfo);
            return obj;
        }
    }

    /**
     * 根据公司id查询公司信息
     * @return
     */
    @PostMapping(value = "/query_companyById")
    public JSONObject query_companyById(String companyid){
        JSONObject obj = new JSONObject();//返回对象
        CompanyInfo companyInfo = userservice.queryCompanyinfoById(companyid);
        if(companyInfo==null){
            obj.put("code", 1);
            obj.put("msg", "暂无公司信息");
            obj.put("data", companyInfo);
            return obj;
        }else {
            obj.put("code", 0);
            obj.put("msg", "查询成功");
            obj.put("data", companyInfo);
            return obj;
        }
    }

    /**
     * 根据公司id修改公司信息
     * @return
     */
    @PostMapping(value = "/update_company_message")
    @Transactional(rollbackFor = Exception.class)
    public JSONObject update_company_message(CompanyInfo companyInfo){
        JSONObject obj = new JSONObject();//返回对象
        MlccUserInfo userInfo = userservice.queryUserinfoByusername(companyInfo.getToken(), 3);
        if (userInfo == null) {
            obj.put("code", 4);
            obj.put("msg", "token已失效，请重新登录");
            return obj;
        }
        if(companyInfo.getCompanyName()==null || "".equals(companyInfo.getCompanyName())){
            obj.put("code", 3);
            obj.put("msg", "公司名称不能为空");
            return obj;
        }
        if(companyInfo.getAboutPerson()==null || "".equals(companyInfo.getAboutPerson())){
            obj.put("code", 3);
            obj.put("msg", "公司联系人不能为空");
            return obj;
        }
        if(companyInfo.getAboutPhone()==null || "".equals(companyInfo.getAboutPhone())){
            obj.put("code", 3);
            obj.put("msg", "公司联系电话不能为空");
            return obj;
        }
        int code = userservice.update_companyinfo(companyInfo);
        if(code>0){
            obj.put("code", 0);
            obj.put("msg", "修改成功");
            return obj;
        }else {
            obj.put("code", 1);
            obj.put("msg", "修改失败");
            return obj;
        }
    }

    /**
     * 根据公司id查询所有门店
     * @return
     */
    @PostMapping(value = "/query_allStoreById")
    @Transactional(rollbackFor = Exception.class)
    public JSONObject query_allStoreById(String companyid){
        JSONObject obj = new JSONObject();//返回对象
        List<StoreInfo> datalist = userservice.queryAllStoreinfoById(companyid);
        if(datalist.size()>0){
            obj.put("code", 0);
            obj.put("msg", "查询成功");
            obj.put("data", datalist);
            return obj;
        }else {
            obj.put("code", 1);
            obj.put("msg", "暂无数据");
            obj.put("data", datalist);
            return obj;
        }
    }

    /**
     * 根据公司id查询所有用户信息
     * @return
     */
    @PostMapping(value = "/query_alluserById")
    public JSONObject query_alluserById(String companyid){
        JSONObject obj = new JSONObject();//返回对象
        List<MlccUserInfo> datalist = userservice.queryAllUserinfoById(companyid);
        if(datalist.size()>0){
            obj.put("code", 0);
            obj.put("msg", "查询成功");
            obj.put("data", datalist);
            return obj;
        }else {
            obj.put("code", 1);
            obj.put("msg", "暂无数据");
            obj.put("data", datalist);
            return obj;
        }
    }

    /**
     * 根据用户id修改用户信息---密码
     * @return
     */
    @PostMapping(value = "/update_userInfoById")
    @Transactional(rollbackFor = Exception.class)
    public JSONObject post_update_userInfoById(WebUserinfo userinfo1){
        JSONObject obj = new JSONObject();//返回对象
        try {
            MlccUserInfo userInfo = userservice.queryUserinfoByusername(userinfo1.getToken(),3);
            if(userInfo==null){
                obj.put("code", 4);
                obj.put("msg", "token已失效，请重新登录");
                return obj;
            }
            if(userinfo1.getPassword()==null || "".equals(userinfo1.getPassword())){
                obj.put("code", 3);
                obj.put("msg", "用户登录密码不能为空");
                return obj;
            }
            if(userinfo1.getPassword().length()<6){
                obj.put("code", 3);
                obj.put("msg", "登录密码长度不能小于6位");
                return obj;
            }
            System.out.println("手机号_缓存=" + redisUtil.get(userinfo1.getPhone()));
            if (!userinfo1.getPhonecode().equals(redisUtil.get(userinfo1.getPhone()))) {
                obj.put("msg", "手机验证码错误");
                obj.put("code", 3);
                return obj;
            }
            String passwordEncode = PasswordUtil.encrypt(userInfo.getWorkNumber(), userinfo1.getPassword(), userInfo.getSalt());
            //设置密码
            userinfo1.setPassword(passwordEncode);
            userinfo1.setId(userInfo.getId());
            int code = userservice.update_userinfoById(userinfo1);
            if(code>0){
                obj.put("code", 0);
                obj.put("msg", "修改成功！");
                return obj;
            }else {
                obj.put("code", 1);
                obj.put("msg", "修改失败");
                return obj;
            }
        }catch (Exception e){
            e.printStackTrace();
            obj.put("code", 2);
            obj.put("msg", "修改失败,请重新修改");
            return obj;
        }
    }

    /**
     * 根据用户id修改用户信息---更换手机号
     * @return
     */
    @PostMapping(value = "/change_phone")
    @Transactional(rollbackFor = Exception.class)
    public JSONObject post_change_phone(WebUserinfo userinfo1){
        JSONObject obj = new JSONObject();//返回对象
        try {
            MlccUserInfo userInfo = userservice.queryUserinfoByusername(userinfo1.getToken(),3);
            if(userInfo==null){
                obj.put("code", 4);
                obj.put("msg", "token已失效，请重新登录");
                return obj;
            }
            if(userinfo1.getNewphone()==null || "".equals(userinfo1.getNewphone())){
                obj.put("code", 3);
                obj.put("msg", "新的手机号码不能为空");
                return obj;
            }
            System.out.println("手机号_缓存=" + redisUtil.get(userinfo1.getPhone()));
            if (!userinfo1.getPhonecode().equals(redisUtil.get(userinfo1.getPhone()))) {
                obj.put("msg", "手机验证码错误");
                obj.put("code", 3);
                return obj;
            }
            if (userinfo1.getNewphone().equals(userInfo.getPhone())) {
                obj.put("msg", "该手机号您已经绑定过了");
                obj.put("code", 3);
                return obj;
            }
            MlccUserInfo userInfo_phone = userservice.queryUserinfoByusername(userinfo1.getNewphone(),2);
            if(userInfo_phone!=null){
                obj.put("msg", "该手机号系统已存在，请换新的号码");
                obj.put("code", 3);
                return obj;
            }
            userinfo1.setId(userInfo.getId());
            int code = userservice.change_phone(userinfo1);
            if(code>0){
                obj.put("code", 0);
                obj.put("msg", "更换成功！");
                return obj;
            }else {
                obj.put("code", 1);
                obj.put("msg", "更换失败");
                return obj;
            }
        }catch (Exception e){
            e.printStackTrace();
            obj.put("code", 2);
            obj.put("msg", "修改失败,请重新修改");
            return obj;
        }
    }

    /**
     * 根据用户id修改用户信息---更换用户名
     * @return
     */
    @PostMapping(value = "/change_user_realname")
    @Transactional(rollbackFor = Exception.class)
    public JSONObject change_user_realname(WebUserinfo userinfo1){
        JSONObject obj = new JSONObject();//返回对象
        try {
            MlccUserInfo userInfo = userservice.queryUserinfoByusername(userinfo1.getToken(),3);
            if(userInfo==null){
                obj.put("code", 4);
                obj.put("msg", "token已失效，请重新登录");
                return obj;
            }
            if(userinfo1.getRealname()==null || "".equals(userinfo1.getRealname())){
                obj.put("code", 3);
                obj.put("msg", "新的用户名不能为空");
                return obj;
            }
            System.out.println("手机号_缓存=" + redisUtil.get(userInfo.getPhone()));
            if (!userinfo1.getPhonecode().equals(redisUtil.get(userInfo.getPhone()))) {
                obj.put("msg", "手机验证码错误");
                obj.put("code", 3);
                return obj;
            }
            userinfo1.setId(userInfo.getId());
            int code = userservice.change_user_realname(userinfo1);
            if(code>0){
                obj.put("code", 0);
                obj.put("msg", "修改成功！");
                return obj;
            }else {
                obj.put("code", 1);
                obj.put("msg", "修改失败");
                return obj;
            }
        }catch (Exception e){
            e.printStackTrace();
            //手动回滚事务
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            obj.put("code", 2);
            obj.put("msg", "修改失败,请重新修改");
            return obj;
        }
    }

    /**
     * 在门店下面创建员工
     * @return
     */
    @PostMapping(value = "/insert_userinfo")
    @Transactional(rollbackFor = Exception.class)
    public JSONObject post_insert_userinfo(WebUserinfo mlccUserInfo){
        JSONObject obj = new JSONObject();//返回对象
        try {
            MlccUserInfo userInfo = userservice.queryUserinfoByusername(mlccUserInfo.getToken(),3);
            if(userInfo==null){
                obj.put("code", 4);
                obj.put("msg", "token已失效，请重新登录");
                return obj;
            }
            if(mlccUserInfo.getUsername()==null || "".equals(mlccUserInfo.getUsername())){
                obj.put("code", 3);
                obj.put("msg", "登录账号不能为空");
                return obj;
            }
            if(mlccUserInfo.getPassword()==null || "".equals(mlccUserInfo.getPassword())){
                obj.put("code", 5);
                obj.put("msg", "登录密码不能为空");
                return obj;
            }
            Long startTs = System.currentTimeMillis(); // 当前时间戳
            //8位随机数
            int random=new Random().nextInt(100)+1;//为变量赋随机值1-99999999;
            String worknumber = random+(startTs.toString());
            //设置密码眼
            String salt = oConvertUtils.randomGen(8);
            mlccUserInfo.setSalt(salt);
            String passwordEncode = PasswordUtil.encrypt(worknumber, mlccUserInfo.getPassword(), salt);
            //设置密码
            mlccUserInfo.setPassword(passwordEncode);
            //设置工号
            mlccUserInfo.setWorkNumber(worknumber);
            //设置token
            String token = JwtUtil.sign(userInfo.getUsername(), userInfo.getPassword());
            mlccUserInfo.setToken(token);

            int code = userservice.insert_userinfo(mlccUserInfo);
            if(code>0){
                obj.put("code", 0);
                obj.put("msg", "新增员工成功！");
                return obj;
            }else {
                obj.put("code", 1);
                obj.put("msg", "新增员工失败");
                return obj;
            }
        }catch (Exception e){
            obj.put("code", 2);
            obj.put("msg", "新增员工失败，请按要求输入信息");
            return obj;
        }
    }

    /**
     * 在门店下面编辑员工
     * @return
     */
    @PostMapping(value = "/update_userinfo")
    @Transactional(rollbackFor = Exception.class)
    public JSONObject post_update_userinfo(WebUserinfo mlccUserInfo){
        JSONObject obj = new JSONObject();//返回对象
        try {
            MlccUserInfo userInfo = userservice.queryUserinfoByusername(mlccUserInfo.getToken(),3);
            if(userInfo==null){
                obj.put("code", 4);
                obj.put("msg", "token已失效，请重新登录");
                return obj;
            }
            if(mlccUserInfo.getUsername()==null || "".equals(mlccUserInfo.getUsername())){
                obj.put("code", 3);
                obj.put("msg", "登录账号不能为空");
                return obj;
            }
            if(mlccUserInfo.getPassword()==null || "".equals(mlccUserInfo.getPassword())){
                obj.put("code", 5);
                obj.put("msg", "登录密码不能为空");
                return obj;
            }
            //根据用户id查询
            MlccUserInfo userInfo1 = userservice.queryUserinfoByusername(mlccUserInfo.getToken(),0);
            //设置密码眼
            String salt = userInfo1.getSalt();
            mlccUserInfo.setSalt(salt);
            String passwordEncode = PasswordUtil.encrypt(userInfo1.getWorkNumber(), mlccUserInfo.getPassword(), salt);
            mlccUserInfo.setPassword(passwordEncode);
            int code = userservice.update_userinfo(mlccUserInfo);
            if(code>0){
                obj.put("code", 0);
                obj.put("msg", "修改员工成功！");
                return obj;
            }else {
                obj.put("code", 1);
                obj.put("msg", "修改员工失败");
                return obj;
            }
        }catch (Exception e){
            obj.put("code", 2);
            obj.put("msg", "修改员工失败，请按要求输入信息");
            return obj;
        }
    }

    /**
     * 用户充值逻辑
     * @return
     */
    @PostMapping(value = "/charge_balance")
    @Transactional(rollbackFor = Exception.class)
    public JSONObject post_charge_balance(@RequestParam(name="token")String token,
                                          @RequestParam(name="charge_method")String charge_method,
                                          @RequestParam(name="money")String money){
        JSONObject obj = new JSONObject();//返回对象
//        try {
            MlccUserInfo userInfo = userservice.queryUserinfoByusername(token,3);
            if(userInfo==null){
                obj.put("code", 4);
                obj.put("msg", "token已失效，请重新登录");
                return obj;
            }
            if(charge_method==null || "".equals(charge_method)){
                obj.put("code", 3);
                obj.put("msg", "请选择充值方式");
                return obj;
            }
            if(money==null || "".equals(money)){
                obj.put("code", 5);
                obj.put("msg", "充值金额不能为空");
                return obj;
            }
            //拿到用户原余额
            String oldbalance = userInfo.getAccountBalance();
            //8位随机数
            int random=new Random().nextInt(1000)+1;//为变量赋随机值1-99999999;
            int random2=new Random().nextInt(1000000)+1;//为变量赋随机值1-99999999;
            //先创建充值订单
            //创建充值订单编号
            String uuid = UUID.randomUUID().toString().substring(0,12);
            String charge_sn = "C"+uuid+random;
            String finial_sn = uuid+random2;//财务流水号
            ChargeOrderInfo chargeOrderInfo = new ChargeOrderInfo();
            chargeOrderInfo.setChargeSn(charge_sn);
            chargeOrderInfo.setApplyMethod(charge_method);
            chargeOrderInfo.setUserId(userInfo.getId().toString());
            chargeOrderInfo.setMoney(money);
            chargeOrderInfo.setStatus(1);//假设充值成功
            userservice.create_chargeOrder(chargeOrderInfo);
            //根据充值订单流水号查询（已充值就修改用户余额）
            ChargeOrderInfo datainfoDetail = userservice.queryChargeOrderInfo(charge_sn);
            int code2=0,code3=0;
            //修改用户余额
            if(datainfoDetail!=null){
                if(datainfoDetail.getStatus()==1){//充值成功
                    //赋值新的余额
                    double newbalance = Double.parseDouble(oldbalance)+Double.parseDouble(money);
                     code2 = userservice.update_user_balance(newbalance+"",userInfo.getId().toString());
                    //插入财务流水表
                     code3 = userservice.insert_financial(userInfo.getId().toString(),finial_sn,"前端用户充值",money,
                            newbalance+"");
                }
            }
            if(code2>0 && code3>0){
                obj.put("code", 0);
                obj.put("msg", "充值成功！");
                return obj;
            }else {
                obj.put("code", 1);
                obj.put("msg", "充值失败");
                return obj;
            }
//        }catch (Exception e){
//            obj.put("code", 2);
//            obj.put("msg", "充值失败，请按要求输入信息");
//            return obj;
//        }
    }


    /**
     * 用户注销登录
     * @return
     */
    @PostMapping(value = "/web_user_logout")
    public JSONObject post_user_logout(String token){
        JSONObject obj = new JSONObject();//返回对象
        MlccUserInfo userInfo = userservice.queryUserinfoByusername(token,3);
        if(userInfo==null){
            obj.put("code", 4);
            obj.put("msg", "token已失效，请重新登录");
            return obj;
        }else {
            //8位随机数
            int random2=new Random().nextInt(1000000)+1;//为变量赋随机值1-99999999;
            //先创建充值订单
            //创建充值订单编号
            String uuid = UUID.randomUUID().toString().substring(0,12);
            String finial_sn = uuid+random2;//财务流水号
            //根据用户id注销token
            userservice.update_token(finial_sn,userInfo.getId().toString(),3);
            //清空用户登录Token缓存
            redisUtil.del(CommonConstant.PREFIX_USER_TOKEN + token);
            //清空用户登录Shiro权限缓存
            redisUtil.del(CommonConstant.PREFIX_USER_SHIRO_CACHE + userInfo.getId());
            //调用shiro的logout
            SecurityUtils.getSubject().logout();
            obj.put("code", 0);
            obj.put("msg", "注销成功");
            return obj;
        }
    }

    /**
     *上传图片公共接口
     * @return
     * @throws
     */
    @PostMapping(value = "/uploadImage")
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.SUPPORTS)
    public JSONObject uploadImage(HttpServletRequest request) throws Exception {
        String path = "";
        JSONObject obj = new JSONObject();// 返回对象
        String key = "";
        String fileNames = "";
        try {
            StandardMultipartHttpServletRequest req = (StandardMultipartHttpServletRequest) request;
            Iterator<String> iterator = req.getFileNames();
            while (iterator.hasNext()) {
                MultipartFile file = req.getFile(iterator.next());
                fileNames = String.valueOf(System.currentTimeMillis())
                        + FileNameUtils.getSuffix(file.getOriginalFilename());
                InputStream input = file.getInputStream();
                // 创建OSSClient实例
                @SuppressWarnings("deprecation")
                OSSClient ossClient = new OSSClient(ENDPOINT, ACCESSKEYID, ACCESSKEYSECRET);
                // 上传文件流
                ossClient.putObject(BUCKETNAME, KEY + fileNames, input);
                ossClient.shutdown();
            }
            path = "https://mlcc-public-001.oss-cn-shanghai.aliyuncs.com/" + KEY + key + fileNames;

            System.out.println(("图片上传阿里云 name=" + key + fileNames));
            obj.put("code", 0);
            obj.put("msg", "上传成功");
            obj.put("data", path);
            return obj;
        } catch (IOException e) {
            e.printStackTrace();
            obj.put("code", 1);
            obj.put("msg", "上传失败,请上传正确的图片");
            obj.put("data", "");
            return obj;
        }
    }


}
